<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2013
 * ---
 * Repraesentiert einen Artikel
 * Methoden, um Artikel aus der DB abzurufen
 *
 * @author Robin
 */

class Article {
    private $id = null;
    private $author = null;
    private $created = null;
    private $modified = null;
    private $title = null;
    private $content = null;
    private $site = null;
    private $imagelink = null;

    public function __construct($data = array()) {
        if (isset($data['article_id'])) $this->id = (int) $data['article_id'];
        if (isset($data['article_author'])) $this->author = (int) $data['article_author'];
        if (isset($data['article_created'])) $this->created = $data['article_created'];
        if (isset($data['article_modified'])) $this->modified = $data['article_modified'];
        if (isset($data['article_title'])) $this->title = preg_replace( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['article_title']);
        if (isset($data['article_content'])) $this->content = $data['article_content'];
        if (isset($data['article_site'])) $this->site = (bool) $data['article_site'];
        if (isset($data['article_imagelink'])) $this->imagelink = $data['article_imagelink'];
    }

    /**
     * 
     * @param type $id
     * @return \Article
     */
    public static function getArticleById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_articles WHERE article_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Article($row);
    }

    /**
     * 
     * @param type $numRows
     * @param type $order
     * @return type
     */
    public static function getArticleList($numRows=1000000, $order="article_created DESC") {
        $db = new Database();

        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM ff_articles ORDER BY " .mysql_real_escape_string($order)." LIMIT :numRows";

        $stmt = $db->prepare( $sql );
        $stmt->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $article = new Article($row);
            $list[] = $article;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;

        return (array("articleList" => $list, "totalRows" => $totalRows[0]));
    }
    
    /**
     * 
     * @param type $id
     * @return type
     */
    public static function getArticleListByCategory($id, $numRows=1000000) {
        $db = new Database();

        $sql = "SELECT a.* FROM ff_articles a, ff_categories c, ff_categories_articles ca WHERE a.article_id = ca.article_id AND c.category_id = ca.category_id AND c.category_id = :id LIMIT :numRows";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $article = new Article($row);
            $list[] = $article;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("articleList" => $list, "totalRows" => $totalRows[0]));
    }
    
    /**
     * 
     */
    public function insert() {
        if (!is_null($this->id)) {
            trigger_error("Article::insert(): Es wird versucht einen Artikel zu speichern, der eine ID hat, die bereits existiert (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_articles (article_author, article_created, article_modified, article_title, article_content, article_site, article_imagelink) VALUES (:author, :created, :modified, :title, :content, :site, :imagelink)";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":author", $this->author, PDO::PARAM_INT);
        $stmt->bindValue(":created", $this->created, PDO::PARAM_STR);
        $stmt->bindValue(":modified", $this->modified, PDO::PARAM_STR);
        $stmt->bindValue(":title", $this->title, PDO::PARAM_STR);
        $stmt->bindValue(":content", $this->content, PDO::PARAM_STR);
        $stmt->bindValue(":site", $this->site, PDO::PARAM_INT);
        $stmt->bindValue(":imagelink", $this->imagelink, PDO::PARAM_STR);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
    
    /**
     * 
     */
    public function update() {
        if (is_null($this->id)) {
            trigger_error("Article::update(): Es wird versucht einen Artikel zu speichern, der keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_articles SET article_author=:author, article_modified=:modified, article_title=:title, article_content=:content, article_site:=site, article_imagelink:=imagelink WHERE article_id=:id";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":author", $this->author, PDO::PARAM_INT);
        $stmt->bindValue(":modified", $this->modified, PDO::PARAM_STR);
        $stmt->bindValue(":title", $this->title, PDO::PARAM_STR);
        $stmt->bindValue(":content", $this->content, PDO::PARAM_STR);
        $stmt->bindValue(":site", $this->site, PDO::PARAM_INT);
        $stmt->bindValue(":imagelink", $this->imagelink, PDO::PARAM_STR);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    
    /**
     * 
     */
    public function delete() {
        if (is_null($this->id)) {
            trigger_error("Article::delete(): Es wird versucht einen Artikel zu loeschen, der keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_articles WHERE article_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }


    public function getAuthor()
    {
        return $this->author;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function getCreated()
    {
        return $this->created;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getMenu()
    {
        return $this->menu;
    }

    public function getModified()
    {
        return $this->modified;
    }

    public function getSite()
    {
        return $this->site;
    }

    public function getTitle()
    {
        return $this->title;
    }
    
    public function getImagelink() {
        return $this->imagelink;
    }


}

?>
